package health_system.health_system.config;

import jakarta.annotation.PostConstruct;
import jakarta.servlet.ServletContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.support.DefaultHandshakeHandler;

@Slf4j
@Configuration
public class AppConfig {

    @Value("${server.servlet.context-path:/api}")
    private String contextPath;
    
    @PostConstruct
    public void init() {
        // Log the application's context path for debugging
        log.info("Application initialized with context path: {}", contextPath);
        
        // Set a system property that can be accessed by WebSocket components
        System.setProperty("app.context-path", contextPath);
    }
    
    /**
     * This bean ensures that the DefaultHandshakeHandler is aware of our context path
     */
    @Bean
    public DefaultHandshakeHandler defaultHandshakeHandler(ServletContext servletContext) {
        DefaultHandshakeHandler handler = new DefaultHandshakeHandler();
        log.info("Configured DefaultHandshakeHandler with context path: {}", contextPath);
        log.info("Servlet context path: {}", servletContext.getContextPath());
        return handler;
    }
} 